package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.ResponseResult;
import com.example.domain.entity.User;
import com.example.enums.AppHttpCodeEnum;
import com.example.exception.SystemException;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult register(User user) {
        //对数据进行非空判断
        if(!StringUtils.hasText(user.getUsername())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getUsertag())){
            throw new SystemException(AppHttpCodeEnum.USERTAG_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getProvince())){
            throw new SystemException(AppHttpCodeEnum.PROVINCE_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getCity())){
            throw new SystemException(AppHttpCodeEnum.CITY_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getAge().toString())){
            throw new SystemException(AppHttpCodeEnum.AGE_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getJob())){
            throw new SystemException(AppHttpCodeEnum.JOB_NOT_NULL);
        }

        //对数据进行是否存在的判断
        if(userNameExist(user.getUsername())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        //...
        //对密码进行加密
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        //存入数据库
        userMapper.insert(user);
        return ResponseResult.okResult(user);
    }

    @Override
    public ResponseResult selectByAge() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("age,count(*) as age_count")
                .orderByAsc("age")
                .groupBy("age");

        List<Map<String,Object>> list = userMapper.selectMaps(wrapper);
        if(Objects.isNull(list)){
            throw new RuntimeException("数据为空");
        }
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult selectByProvince() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("province,count(*) as pro_count")
                .groupBy("province");

        List<Map<String,Object>> list = userMapper.selectMaps(wrapper);
        if(Objects.isNull(list)){
            throw new RuntimeException("数据为空");
        }
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult selectByTag() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("userTag,count(*) as tag_count")
                .groupBy("userTag");

        List<Map<String,Object>> list = userMapper.selectMaps(wrapper);
        if(Objects.isNull(list)){
            throw new RuntimeException("数据为空");
        }
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult selectByJob() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("job,count(*) as job_count")
                .groupBy("job");

        List<Map<String,Object>> list = userMapper.selectMaps(wrapper);
        if(Objects.isNull(list)){
            throw new RuntimeException("数据为空");
        }
        return ResponseResult.okResult(list);
    }

    private boolean userNameExist(String username){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        int result = userMapper.selectCount(queryWrapper);
        return result>0;
    }


}
